package com.SampleListView;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.db.MyDatabase;


public class UserDetailLoader {
	
	private static final String TAG = "FolderDetailLoader";
	
	private Context mContext;
	private MyDatabase mDatabase;
	private Handler mHandler;
	private ExecutorService mLoaderService = Executors.newSingleThreadExecutor();
	
	public UserDetailLoader(Context context) {
		mContext = context;
		mDatabase = new MyDatabase(mContext);
		mHandler = new Handler();
	}

	public void loadDetailAsync(String userId, UserDetailCallback loadedCallback) {
		final String userIdFinal = userId;
		final UserDetailCallback loadedCallbackFinal = loadedCallback;
		mLoaderService.execute(new Runnable() {
			@Override
			public void run() {
				try {
				final UserInfo folderInfo = mDatabase.getFolderInfoFull(userIdFinal);
				
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						loadedCallbackFinal.onLoaded(folderInfo);
					}
				});
				} catch (Exception e) {
					Log.e(TAG, "Error occured while loading user Detail info.", e);
				}
			}
		});
	}
	
	public void destroy() {
		mLoaderService.shutdownNow();
		mDatabase.close();
	}
	
	public interface UserDetailCallback {
		void onLoaded(UserInfo folderInfo);
	}
}
